//
//  HotIndustriesDetailVC.m
//  RedManStock
//
//  Created by lanou3g on 15/7/28.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HotIndustriesDetailVC.h"
#import "HotIndusrtiesDetailTableViewCell.h"
#import "MyHandle.h"
#import "UpList.h"
#import "StockHomeViewController.h"
#import "MBProgressHUD.h"
@interface HotIndustriesDetailVC ()

@property(nonatomic,retain)NSMutableArray *upListArray;
@end

@implementation HotIndustriesDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.upListArray = [NSMutableArray array];
//    self.update= YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"HotIndusrtiesDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"hotIndustries"];
    
     [self loadNewData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//-(void)viewWillAppear:(BOOL)animated{
//    
//    if (self.update) {
//        
//        [self loadNewData];
//    }
//    
//}



#pragma mark- 下拉刷新（请求数据，结束header） -
-(void)loadNewData{
    //    出现大菊花
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    if (self.which) {
        
        NSString *url = @"http://api.gongniucaijing.com/quotation/ranking";
        [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
            
            NSDictionary *dic  = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                                   @"id":self.ID,
                                   @"command":@"0",
                                   @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.upListArray.count],
                                   @"number":@"50",
                                   @"type":@"industries"
                                   };
            return dic;
        } successBlock:^(NSDictionary *responseDic) {
            [self dataSourceWithJasonDic:responseDic];
            [self.tableView.header endRefreshing];
        } errorBlock:^(NSError *error) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            
            [alertView show];
            [self.tableView.header endRefreshing];

            
        }];
        
    }else{
        
        NSString *url = @"http://api.gongniucaijing.com/quotation/ranking";
        [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
            
            NSDictionary *dic  = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                                   @"id":self.ID,
                                   @"command":@"0",
                                   @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.upListArray.count],
                                   @"number":@"50",
                                   @"type":@"concept"
                                   };
            return dic;
        } successBlock:^(NSDictionary *responseDic) {
            [self dataSourceWithJasonDic:responseDic];
            [self.tableView.header endRefreshing];
        } errorBlock:^(NSError *error) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            
            [alertView show];
            [self.tableView.header endRefreshing];

            
            
        }];
        
        
        
        
    }
    
        
}




-(void)loadMoreData{
    
    if (self.which) {
        
        NSString *url = @"http://api.gongniucaijing.com/quotation/ranking";
        [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
            
            NSDictionary *dic  = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                                   @"id":self.ID,
                                   @"command":@"1",
                                   @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.upListArray.count],
                                   @"number":@"50",
                                   @"type":@"industries"
                                   };
            return dic;
        } successBlock:^(NSDictionary *responseDic) {
            [self dataSourceWithJasonDic:responseDic];
            [self.tableView.header endRefreshing];
        } errorBlock:^(NSError *error) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            
            [alertView show];
            [self.tableView.header endRefreshing];

            
        }];
        
    }else{
        
        NSString *url = @"http://api.gongniucaijing.com/quotation/ranking";
        [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
            
            NSDictionary *dic  = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                                   @"id":self.ID,
                                   @"command":@"1",
                                   @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.upListArray.count],
                                   @"number":@"50",
                                   @"type":@"concept"
                                   };
            return dic;
        } successBlock:^(NSDictionary *responseDic) {
            [self dataSourceWithJasonDic:responseDic];
            [self.tableView.header endRefreshing];
        } errorBlock:^(NSError *error) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            
            [alertView show];
            [self.tableView.header endRefreshing];

            
        }];
        
        
        
    }
    
    
    
    
}






#pragma mark- 共用方法：数据源赋值，reloadData -
- (void)dataSourceWithJasonDic:(NSDictionary *)dic{
    
    NSDictionary *dataDic = [dic objectForKey:@"data"];
    NSArray *listsArray = [dataDic objectForKey:@"lists"];
    for (NSDictionary *dic in listsArray) {
        
        UpList *uplist = [[UpList alloc]init];
        [uplist setValuesForKeysWithDictionary:dic];
        [self.upListArray addObject:uplist];
        
    }
    
    //        结束大菊花
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    [self.tableView reloadData];
    
}












#pragma mark- 分行 -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.upListArray.count;
}
#pragma mark- 分区 -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
}


#pragma mark - cell复用 -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        HotIndusrtiesDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotIndustries" forIndexPath:indexPath];
    cell.upList = [self.upListArray objectAtIndex:indexPath.row];
    
    
    return cell;
}



-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    NSString *title = @"名称代码     最新价    涨跌幅    推荐数";
    return  title;
}


#pragma mark - cell被选中方法 -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    StockHomeViewController *stockHome = [[StockHomeViewController alloc]init];
    UpList *upList = self.upListArray[indexPath.row];
    stockHome.stockCode = upList.codeSB;
    stockHome.stockName = upList.name;
    [self.navigationController pushViewController:stockHome animated:YES];
    
    
    
}









/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
